package com.tanhua.server.controller;

import com.tanhua.server.service.VoiceService;
import com.tanhua.server.vo.UserVoice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author zhoucheng
 * @version 1.0
 * @date 2021/3/4 9:04
 */
@RestController
@RequestMapping("peachblossom")
public class VoiceController {

    @Autowired
    private VoiceService voiceService;
    /**
     * 桃花传音-发送语音
     * @param soundFile
     * @return
     */
    @PostMapping
    public ResponseEntity<String> sendVoice(@RequestParam(value = "soundFile") MultipartFile soundFile){
        try {
            Boolean bool = this.voiceService.sendVoice(soundFile);//发送语音是否成功
            if (bool) {
                return ResponseEntity.ok("发送成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 接收语音
     */
    @GetMapping
    public ResponseEntity<UserVoice> getVoice() {
        try {
            UserVoice userVoice = this.voiceService.getVoice();
            if (userVoice != null) {
                return ResponseEntity.ok(userVoice);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
